#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QDebug>
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <stack>
#include <sstream>

using namespace std;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
    bool uiInit();
    bool updateText(QString _text);
    bool wordAnalysis(vector<pair<string, int>>& word, QString expr);

    vector<pair<string, int>> word;

    QString _text_ = "";

    int idx = 0;
    int sym;
    int err = 0; // 错误

    void E();
    void E1();
    void T();
    void T1();
    void F();
    void Next();

    int prior(int token);
    bool isOperator(int token);
    vector<pair<string, int>> getPostfix(vector<pair<string, int>> expr);
    void popTwoNum(stack<double> &numStack, double &first, double &second);
    double getAnswer(const vector<pair<string, int>> &postfix);

private:
    Ui::Widget *ui;
    QPushButton *num1;
    QPushButton *num2;
    QPushButton *num3;
    QPushButton *num4;
    QPushButton *num5;
    QPushButton *num6;
    QPushButton *num7;
    QPushButton *num8;
    QPushButton *num9;
    QPushButton *num0;

    QPushButton *_dot;
    QPushButton *_back;
    QPushButton *_equal;

    QPushButton *_add;
    QPushButton *_sub;
    QPushButton *_mul;
    QPushButton *_div;

    QPushButton *_leftbracket;
    QPushButton *_clean;
    QPushButton *_rightbracket;

    QLabel *_output;

    QString _zero = "0";

private slots:
    void getNum1();
    void getNum2();
    void getNum3();
    void getNum4();
    void getNum5();
    void getNum6();
    void getNum7();
    void getNum8();
    void getNum9();
    void getNum0();
    void getAdd();
    void getSub();
    void getMul();
    void getDiv();
    void getLBracket();
    void getRBracket();
    void getBackOption();
    void getDot();
    void getClean();
    void calculate();


};

#endif // WIDGET_H
